package com.xm.zcf.dao.product.impl;

import com.xm.zcf.action.Base.SessionBase;
import com.xm.zcf.dao.product.ProductDao;
import com.xm.zcf.domain.product.Product;
import com.xm.zcf.util.PageBean;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */
@Repository
public class ProductDaoImpl extends SessionBase implements ProductDao {
    @Override
    public List<Product> findAll() {
        return getSession().createQuery("from Categorysecond ").list();
    }

    @Override
    public PageBean<Product> findPage(Integer page, Integer limit) {
        List<Product>list=getSession().createQuery("from Product ")
                .setFirstResult((page-1)*10)
                .setMaxResults(limit)
                .list();
        Long count=(Long)getSession().createQuery("select count(*) from Product").uniqueResult();
        return new PageBean<Product>(page,limit,count.intValue(),list);
    }

    @Override
    public Product findById(int pid) {
        return (Product) getSession().createQuery("from Product p where p.pid=?")
                .setParameter(0,pid)
                .uniqueResult();
    }

    @Override
    public void save(Product model) {
        getSession().save(model);
    }

    @Override
    public List<Product> findHotProduct(int i) {
        return getSession().createQuery("from Product where isHot=?")
                .setParameter(0,1)
                .setFirstResult(0)
                .setMaxResults(i)
                .list();
    }

    @Override
    public List<Product> findNewProducts(int i) {
        return getSession().createQuery("from Product order by pdate desc")
                .setFirstResult(0)
                .setMaxResults(i)
                .list();
    }

    @Override
    public PageBean<Product> findPageBean(Integer page, Integer limit, Integer cid) {
        Long count =(Long)getSession().createQuery("select count(*) from Product p where p.categorySecond.category.cid=?")
                .setParameter(0,cid)
                .uniqueResult();

        List list=getSession().createQuery(
                "from Product p where p.categorySecond.category.cid=?")
                .setParameter(0,cid)
                .setFirstResult((page-1)*limit)
                .setMaxResults(limit)
                .list();
        return new PageBean(page,limit,count.intValue(),list);
    }

    @Override
    public Product findByPid(Integer id) {
        return (Product)getSession().createQuery("from Product p where p.pid=?").setParameter(0,id).uniqueResult();
    }

    @Override
    public PageBean<Product> findCsPageBean(Integer page, Integer limit, Integer csid) {
        Long count =(Long)getSession().createQuery("select count(*) from Product p where p.categorySecond.csid=?")
                .setParameter(0,csid)
                .uniqueResult();

        List<Product> list=getSession().createQuery(
                "from Product p where p.categorySecond.csid=?")
                .setParameter(0,csid)
                .setFirstResult((page-1)*limit)
                .setMaxResults(limit)
                .list();
        return new PageBean<Product>(page,limit,count.intValue(),list);
    }
}
